package com.learn.core.controller.manager;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.learn.common.annotation.Log;
import com.learn.common.annotation.UserLoginToken;
import com.learn.common.enums.BusinessType;
import com.learn.common.enums.OperatorType;
import com.learn.common.enums.ResultEnum;
import com.learn.common.utils.*;
import com.learn.common.utils.result.CommonResult;
import com.learn.core.domain.LRole;
import com.learn.core.dto.LRoleDTO;
import com.learn.core.service.ILRoleService;
import com.learn.framework.web.controller.WebController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @auther CodeGenerator
 * @create 2021-03-08 22:50:00
 * @describe 角色
 */
@Api("用户登录")
@RestController
@RequestMapping("/manager/role")
public class LRoleController extends WebController<LRole> {

    @Autowired
    private ILRoleService ilRoleService;

    //列表
    @ApiOperation(value = "获取列表")
    @UserLoginToken()
    @PostMapping("/list")
    public PageResult<LRole> list(@RequestBody LRoleDTO lRoleDTO){
        try {
            if (lRoleDTO==null||lRoleDTO.getPage()==null||lRoleDTO.getRows()==null){
                return new PageResult<>(null, null, ResultEnum.ERROR);
            }
            PageHelper.startPage(lRoleDTO.getPage(),lRoleDTO.getRows());
            QueryWrapper<LRole> queryWrapper = new QueryWrapper<>();
            if(StringUtils.isNotEmpty(lRoleDTO.getRoleName()))
                queryWrapper.like("role_name",lRoleDTO.getRoleName());
            if(StringUtils.isNotEmpty(lRoleDTO.getState()))
                queryWrapper.eq("state",lRoleDTO.getState());
            if(lRoleDTO.getSeq()!=null)
                queryWrapper.orderByAsc(lRoleDTO.getSeq());
            Page<LRole> lRoles = (Page<LRole>)ilRoleService.list(queryWrapper);

            if (CollectionUtils.isEmpty(lRoles)) {
                return new PageResult<>(null, null, ResultEnum.NOT_EEIST);
            }
            // 返回
            return new PageResult(lRoles.getTotal(), lRoles, ResultEnum.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new PageResult(null,null,ResultEnum.ERROR);
        }
    }

    //更新
    @ApiOperation(value = "更新")
    @PostMapping("/edit")
    @UserLoginToken
    @Log(title = "更新角色", businessType = BusinessType.UPDATE, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> edit(@RequestBody LRole lRole) {
        if(lRole!=null){
            lRole.setUpdateTime(new Date());
            if(ilRoleService.updateById(lRole)){
                return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS);
            }
        }
        return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }

    //新增
    @ApiOperation(value = "添加")
    @PostMapping("/add")
    @UserLoginToken
    @Log(title = "添加角色", businessType = BusinessType.INSERT, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> add(@RequestBody LRole lRole){
        try {
            if(lRole!=null) {
                if (StringUtils.isEmpty(lRole.getRoleName())) {
                    return (CommonResult) ResultUtil.error(ResultEnum.NOT_ROLE_NAME);
                }
                lRole.setCreateTime(new Date());
                if (ilRoleService.save(lRole)) {
                    return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS);
                }
            }
        }catch (Exception e){
            return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
        }
        return  (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }


    //删除
    @ApiOperation(value = "删除")
    @PostMapping("/del")
    @UserLoginToken
    @Log(title = "删除角色", businessType = BusinessType.DELETE, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> del(@RequestBody LRole lRole){
        try {
            if(lRole!=null){
                if(lRole.getRoleId()!=null){
                    QueryWrapper<LRole> queryWrapper = new QueryWrapper<LRole>();
                    queryWrapper.in(true, "role_id", lRole.getRoleId());
                    if(ilRoleService.remove(queryWrapper)){
                        return (CommonResult)ResultUtil.success(ResultEnum.SUCCESS);
                    }
                }
            }
        }catch (Exception e)
        {
            return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
        }
        return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }
}
